/*
 * TimestampHelper.java
 *
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2014 "Tigase, Inc." <office@tigase.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 */
package tigase.archive;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author andrzej
 */
public class TimestampHelper {
	
	private final static SimpleDateFormat formatter4 = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
	private final static SimpleDateFormat formatter3 = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
	private final static SimpleDateFormat formatter2 = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ssZ");
	private final static SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ss'Z'");
	
	/**
	 * vlep通用时间格式化格式
	 */
	private final static SimpleDateFormat vlep_formatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	private final static SimpleDateFormat vlep_formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
	
	static {
//		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
//		formatter2.setTimeZone(TimeZone.getTimeZone("UTC"));
//		formatter3.setTimeZone(TimeZone.getTimeZone("UTC"));
//		formatter4.setTimeZone(TimeZone.getTimeZone("UTC"));
//		vlep_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
	}
	
	public static void main(String[] args) throws ParseException {
//		System.out.println(getNextTime(new Date(), 8));
//		System.out.println(getNextTime(parseTimestamp("2016-11-11T11:13:52+0800"), 8));
		System.out.println(parseTimestamp("2016-11-11 10:47:37 831"));
//		System.out.println(formatter.format(new Date()));
		System.out.println(formatter2.format(parseTimestamp("2016-11-11 10:47:37 831")));
//		System.out.println(formatter3.format(new Date()));
//		System.out.println(formatter4.format(new Date()));
//		
//		System.out.println(vlep_formatter.format(new Date()));
//		System.out.println(vlep_formatter2.format(new Date()));
		
	}
	
	/**
	 * 获取vlep通用时间校验格式
	 * @title vlepDateFormatter 
	 * @describe TODO
	 * @return 
	 * @author M.simple  
	 * @version 1.0
	 */
	public static SimpleDateFormat vlepDateFormatter(){
		synchronized (vlep_formatter2) {
			return vlep_formatter2;
		}
	}
	
	/**
	 * 过去该时间后X个小时的时间
	 * @title getNextTime 
	 * @describe TODO
	 * @param date
	 * @param timeNumber
	 * @return 
	 * @author M.simple  
	 * @version 1.0
	 */
	public static Date getNextTime(Date date, int timeNumber){
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, 8);
		
		return calendar.getTime();
	}
	
	public static Date parseTimestamp(String tmp) throws ParseException {
		if (tmp == null)
			return null;
		
		tmp = tmp.trim();
		Date date = null;

		if (tmp.endsWith("Z")) {
			if (tmp.contains(".")) {
				synchronized (formatter4) {
					date = formatter4.parse(tmp);
				}
			}
			else { 
				synchronized (formatter) {
					date = formatter.parse(tmp);
				}
			}
		} else if (tmp.contains(".")) {
			synchronized (formatter3) {
				date = formatter3.parse(tmp);
			}
		} else if(tmp.contains(" ")){
			if(tmp.split(" ").length > 2){
				synchronized (vlep_formatter2) {
					date = vlep_formatter2.parse(tmp);
				}
			}else{
				synchronized (vlep_formatter) {
					date = vlep_formatter.parse(tmp);
				}
			}
		}
		else{
			synchronized (formatter2) {   
				date = formatter2.parse(tmp);  
			}
		}

		return date;
	}	
	
	public static String format(Date ts) {
		synchronized (formatter2) {
			return formatter2.format(ts);
		}
	}
	
}
